﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Code
{
    public static class CharEx
    {
        /// <summary>
        /// 将char[]中的元素排序之后转换成字符串形式
        /// </summary>
        /// <param name="list">要排序的数组</param>
        /// <param name="desc">是否为降序</param>
        /// <returns></returns>
        public static string SortToString(this char[] list,bool desc)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            return Sort_private(list,desc);
        }
        private static string Sort_private(char[] list, bool desc)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < list.Length; i++)
            {
                int index = i;
                int j = i + 1;
                for (; j < list.Length; j++)
                {
                    if (desc)
                    {
                        if (list[index] < list[j])
                            index = j;
                    }
                    else
                    {
                        if (list[index] > list[j])
                            index = j;
                    }
                }
                char temp = list[index];
                list[index] = list[i];
                list[i] = temp;
                str.Append(temp);
            }
            return str.ToString();
        }
    }
}
